package com.xiangdao.controller;

import com.xiangdao.model.User;
import com.xiangdao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@Controller
public class RegisterController {
    @Autowired
    UserService userService;

    @RequestMapping(path = "/register", method = RequestMethod.GET)
    public String RegisterUser() {
        return "register";
    }
    @RequestMapping(path = "/register",method = RequestMethod.POST)
    public View RegisterUser(User user, Model model,HttpServletRequest request) {
        String contextPath = request.getContextPath();
        //错误二:输入两次密码前后不一致
        if (!user.getPassword().equals(user.getPassword1())) {
                return new RedirectView(contextPath+"/register");
            } else {
                user.setImage("wu.jpeg");
                userService.addUser(user);
                return new RedirectView(contextPath+"registration_success");
            }

        }

}
